<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Page Config->DB-Restore
 * db-restore
 * use backup.page.class.php
 *     restore.tpl.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2015
 * @author     Norbert Gebert
 * @license    GPL3
 */
function contentPage($refPage, $page, $subpage, $refValidate)
{
	if (isset($_GET['func']) and $_GET['func'] === 'restore')
	{
		if ($refValidate->cleanslash($_REQUEST['submit']) === _('Start database restore'))
		{ // Restore database
			$_SERVER["bmessage"] = _('Restore startet...') . '<br>';
			$file = 'backup/' . $_FILES['uploadfile']['name'];
			
			if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file))
			{
				$_SERVER["bmessage"] .= _('Restorefile successfuly uploaded...' . '<br>');
				include_once('module/class/backup.class.php');
				$backup = new backup();

				if (backup::startRestore($file))
					message::setNoteRow(_('Database restored successfully'), TRUE);
				else
					message::setErrorRow(_('Error restoring the database!'), TRUE);
			}
			else
			{
				$_SERVER["bmessage"] .= _('Restorefile could not uploaded!' . '<br>');
			}

			$refPage->assign('restoreMessage',
				'<span id="bMessage">' .
				$_SERVER["bmessage"] . '</span>');
		}
	}
	else
	{
		$refPage->assign('restoreMessage', '');

		$refPage->assign('btnRestoreDb',
			'<input type="submit" name="submit" id="restore" value="' .
			_('Start database restore') . '" />');
	}

	$refPage->assign('upload', _('Upload file') . ': ');
	$refPage->assign('uploadfile',
		'<input type="file" size="60" name="uploadfile"' .
		'id="uploadfile" />');

	$refPage->assign('text',
		_('First select the backup file to be written to the database. ' .
			'Then click on the "Start database restore" button.<br>' . 
			'After successfully restoring the database, the login page is called. ' .
			'This is not an error but for technical reasons. <br>' . 
			'Depending on the size of the database recovery may take several minutes.'));

	$pagefile = 'module/page/' . $page . '/' . $subpage . '.page.form.php';
	if (is_file($pagefile))
		include_once $pagefile;
}

?>